package com.closer.server.biz.problem.validator;

import com.closer.server.biz.submission.dto.SearchRequest;
import com.closer.server.biz.problem.enums.LanguageEnum;
import com.closer.server.mapper.problem.ProblemMapper;
import com.closer.server.mapper.user.UserMapper;
import com.closer.server.support.exception.ErrorCode;
import com.closer.server.support.exception.ThrowUtils;
import org.springframework.stereotype.Component;

@Component
public class SubmissionValidator {

    public final ProblemMapper problemMapper;

    public final UserMapper userMapper;

    public SubmissionValidator(ProblemMapper problemMapper, UserMapper userMapper) {
        this.problemMapper = problemMapper;
        this.userMapper = userMapper;
    }

    /**
     * 查询提交题目参数
     * @param params 查询提交题目参数
     */
    public void validate(SearchRequest params) {
        if (params.getProblemId() != null){
            ThrowUtils.throwIf(params.getProblemId()<=0, ErrorCode.PARAMS_ERROR, "题目ID不能小于0");
            var question = problemMapper.selectOneById(params.getProblemId());
            ThrowUtils.throwIf(question == null, ErrorCode.PARAMS_ERROR, "题目不存在");
        }
        if(params.getUserId() != null){
            var user = userMapper.getUserInfoById(params.getUserId());
            ThrowUtils.throwIf(user == null, ErrorCode.PARAMS_ERROR, "用户不存在");
        }
        if (params.getLanguage() != null) {
            ThrowUtils.throwIf(LanguageEnum.getEnumByValue(params.getLanguage()) == null, ErrorCode.PARAMS_ERROR, "暂不支持该语言");
        }
    }
}
